/*
 * jQuery Cloudetek plugin
 * Copyright (c) 2011 spletart.com
 *
 * Version : 1.0.0
 *
*/	 
(function($) 	// The $ here signifies a parameter name
        		// As you can see from below, (jQuery) is
        		// immediately passed as the $ param
{
	var host = window.location.protocol; //$(location).attr('host');
	var cloudetekHost = "cloudetek.appspot.com"; 
	if (host == "file:")
		{
			cloudetekHost = "localhost:8888";
		}
	
	var feedUrl = "http://" + cloudetekHost + "/user/service/feed?hostid="+host;
	var serviceUrl = "http://" + cloudetekHost + "/user/service?plain=true&callback=?";
	var apiUrl = "http://" + cloudetekHost + "/api/product?callback=?"
	    		    	
	$.fn.products = function(options, completedCallback)	
	{
		var opts = $.extend({}, $.fn.products.defaults, options);

		start(this);
		
		function start(panel)
		{
	    	$.getJSON(apiUrl,
	    			{
	    				pageSize: 10,
	    				hostid:  opts.hostId
	    			},
					function(data) {

	    				if (!opts.hidePaging)
	    				{
	    					panel.append("<div class=\"pagingPanel\">1 - " + data.list.length + " of  + opts.pageSize + </div>");	    					
	    				}
	    				if (panel.find("ul") == null)
	    				{
	    					panel.append("<ul>");	    					
	    				}

						var listEl = $("ul:first", panel);
						
						$.each(data.list,
								function(i, item) {
							
									// image...
									var width = opts.productThumbHeight;
									var imageUrl = opts.placeholder;
									if (item.photos.length > 0) {
										imageUrl = item.photos[0].photoUrl; 
									}
									
									listEl.append("<li class=\"product\">");
									if (opts.actualWidth){
										var asra = opts.productThumbHeight / item.photos[0].height; 									
										width = Math.round(asra * item.photos[0].width);
										$(".product:last-child").css("width", width + "px");
									}
									
									var title = item.title;
									$(".product:last-child").append("<a title=\"" + item.title + "\" href=\"http://" + cloudetekHost + imageUrl + "&width=" + opts.productImageWidth + "&height=" + opts.productImageHeight + "\"" +
											"<figure><img class=\"product-image\" alt=\"" + item.title + "\" src=\"http://" + cloudetekHost + imageUrl + "&width=" + opts.productThumbWidth + "&height=" + opts.productThumbHeight + "\"><figcaption class=\"product-title\">" + item.title + "</figcaption></figure>");
									// title
									if (opts.hideTitle)
									{
										$(".product:last-child .product-title").hide();
									}
									if (!opts.hidePrice)
									{
										$(".product:last-child").append("<div class=\"product-price\" >" + item.price + " €</div>");
									}
									if (!opts.hideDimensions && (item.dimensionLength != undefined &&  item.dimensionWidth != undefined) )
									{
										$(".product:last-child").append("<div class=\"product-dimensions\" >" + item.dimensionLength + " x " + item.dimensionWidth + "</div>");
									}
	
									if (!opts.hideCart)
									{
										$(".product:last-child").append("<div role=\"button\" alt=\"Add to cart\" tabindex=\"" + i + "\" class=\"googlecart-add-button\">");										
									}
						});
						
						// callback
			     		if(typeof(completedCallback) == 'function'){
				     		completedCallback();
				     	}
						
					});
		}
	};
	
	$.fn.products.defaults = {
			
			pageSize: 10,
			hideTitle: false,
			hidePrice: false,
			hidePaging: true,
			hideDimension: true,
			hideCart: true,
			
			productImageWidth: 1000,
			productImageHeight: 550,
			
			productThumbWidth: 500,
			productThumbHeight: 200,	
			
			actualWidth: false,
			placeholder: "", 
			
			hostId: null
	};
	
})(jQuery); // pass the jQuery object to this function